using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class HelperUtilities 
{
    
    
    
    
    public static Camera mainCamera;
    public static Vector3 GetMouseWorldPosition()
    {
        if (mainCamera == null) mainCamera = Camera.main;

        Vector3 mouseScreenPosition = Input.mousePosition;

        // Clamp mouse position to screen size
        mouseScreenPosition.x = Mathf.Clamp(mouseScreenPosition.x, 0f, Screen.width);
        mouseScreenPosition.y = Mathf.Clamp(mouseScreenPosition.y, 0f, Screen.height);

        Vector3 worldPosition = mainCamera.ScreenToWorldPoint(mouseScreenPosition);

        worldPosition.z = 0f;

        return worldPosition;
    }
    public static float GetAngleFromVector(Vector3 vector)
    {
        float radians = Mathf.Atan2(vector.y, vector.x);
        float degrees = radians * Mathf.Rad2Deg;
        return degrees;
    }
    public static Vector3 GetDirectionVectorFromAngle(float angle)
    {
        Vector3 directionVector = new Vector3(Mathf.Cos(Mathf.Deg2Rad * angle), Mathf.Sin(Mathf.Deg2Rad * angle), 0f);
        return directionVector;
    }
    public static AimDirection GetAimDirection(float angleDegrees)
    {
        AimDirection aimDirection;

        // Set player direction
        // Up Right
        if (angleDegrees >= 22f && angleDegrees <= 67f)
        {
            aimDirection = AimDirection.UpRight;
        }
        // Up
        else if (angleDegrees > 67f && angleDegrees <= 112f)
        {
            aimDirection = AimDirection.Up;
        }
        // Up Left
        else if (angleDegrees > 112f && angleDegrees <= 158f)
        {
            aimDirection = AimDirection.UpLeft;
        }
        // Left
        else if ((angleDegrees <= 180f && angleDegrees > 158f) || (angleDegrees > -180 && angleDegrees <= -135f))
        {
            aimDirection = AimDirection.Left;
        }
      
       
        // Down
        else if (angleDegrees > -135f && angleDegrees <= -45f)
        {
            aimDirection = AimDirection.Down;
        }
        // Down Right
        else if (angleDegrees > -45f && angleDegrees <= 0f||(angleDegrees>0f&&angleDegrees<22f))
        {
            aimDirection = AimDirection.Right;
        }
        else
        {
            aimDirection = AimDirection.Right;
        }

        return aimDirection;
    }


    public static bool ValidateEmptyString(UnityEngine.Object thisObject,string fieldname, string dataString)
    {
        
        if (dataString == "")
        {
            Debug.Log(fieldname + " is empty in " + thisObject.name.ToString());
            
            return true;
        }
        return false;
        
    }
    public static bool ValidateCheckRoomNodeList(UnityEngine.Object thisObject,IEnumerable list)
    {

        int count = 0;
        foreach(var item in list)
        {
            if (item == null)
            {
                Debug.Log((count + 1).ToString() + "th element missing in "+thisObject.name.ToString());
                return true ;
            }
            count++;
           
        }
        if (count == 0)
        {
            Debug.Log("no element in the RoomNodeList");
            return true;
        }

        return false;
    }

    public static bool ValidateCheckNullValue(UnityEngine.Object thisObject, string fieldName, UnityEngine.Object objectToCheck)
    {
        if (objectToCheck == null)
        {
            Debug.Log(fieldName + " is null and must contain a value in object " + thisObject.name.ToString());
            return true;
        }
        return false;
    }

    public static bool ValidateCheckPositiveValue(UnityEngine.Object thisObject, string fieldFieldName, int valueToCheck, bool isZeroAllowed)
    {
        bool error = false;

        if (isZeroAllowed)
        {
            if (valueToCheck < 0)
            {
                Debug.Log(fieldFieldName + " must contain a positive value or zero in object " + thisObject.name);
                error = true;
            }
        }
        else
        {
            if (valueToCheck <= 0)
            {
                Debug.Log(fieldFieldName + " must contain a positive value in object " + thisObject.name);
                error = true;
            }
        }

        return error;
    }

}

